﻿// Custom editor for navigation module
// Provides sortable pages as inline editing

jQuery.fn.cmsModuleEditor_Navigation = function(opts) {
    return this.each(function() {

        // TODO: Permission check for page ordering
        // At the moment any editor can try to reorder but will be prevented by the server's checks

        // Find each individual UL in the specified module and make its items sortable

        $(this).find('ul').each(function() {
            var thisRef = $(this);
            thisRef.sortable({
                containment: 'parent', // Ensures all levels can be ordered independently
                cursor: 'crosshair',
                start: function(event, ui) {
                    // Disable all links when sorting starts so that no accidental clicks can be made
                    // Stores the link's original URL in $(this).data('url')
                    $(ui.item).parent().find('a').each(function() {
                        $(this).data('url', $(this).attr('href'));
                        this.href = 'javascript:void(0);';
                    });
                },
                update: function(event, ui) {
                    // Set a timeout of 50ms so that jQuery has a chance to sort everything out
                    // Without this pause, the hrefs get set back too quickly and can cause accidental clicks
                    $(ui.item).animate({ width: $(ui.item).width() }, 50, function() {

                        // Create an array of page IDs in their new order
                        var pageOrdering = [];
                        $(ui.item).parent().find(' > li').each(function() {
                            // Page ID has been rendered as the rel attribute of each LI by the Navigation Module
                            pageOrdering.push($(this).attr('rel'));
                        });

                        // Work out what the parent page of this ordering set is by traversing up the LI chain
                        var parentId = 'HOME';
                        var parentLI = $(ui.item).parents('li:first');
                        if (parentLI && parentLI.attr('rel')) {
                            parentId = parentLI.attr('rel');
                        }
                        // TODO: Work out what happens when the module itself is contained with an LI - suspect breakage

                        // Send the parent page ID and the newly ordered page ID array to the SetOrder action
                        $.postJSON('/cms/pages/SetOrder', { parent: parentId, ordering: $.compactJSON(pageOrdering) }, function(json) {
                            if (json.success) {
                                // TODO: Inform user of workflow item in Inbox
                            }
                        });

                        // Set the links of URLs back so that the module functions as a menu again
                        $(ui.item).parent().parent().find('a').each(function() {
                            $(this).animate({ width: $(this).width() }, 50, function() {
                                this.href = $(this).data('url');
                            });
                        });
                    });
                }
            });
        });
    });
};
